package org.fzu.ercha.wuta;

import android.app.Application;
import android.content.Context;
import android.content.Intent;

import org.fzu.ercha.wuta.Entity.Audio;
import org.fzu.ercha.wuta.Util.MediaUtil;
import org.fzu.ercha.wuta.Util.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


/**
 * Created by ercha on 2016/5/20.
 */
public class MyApplication extends Application {

    private static Context context;
    //没有载入数据时为 null，否则为 ArrayList
    private static List<Audio> mAudioList = null;
    private static boolean updateListFlag = false;

    private static final String SPNAME= "song";
    private static final String SPKEY_SONG_LIST = "song";
    private static final String SPKEY_CURRENT_SONG = "currentSong";
    private static final String SPKEY_CURRENT_SONG_INDEX = "currentSongIndex";

    private static Integer mCurrentAudioIndex = null;
    private static Audio mCurrentAudio = null;

    public static void setCurrentAudio(int index){
        mCurrentAudioIndex = index;
        mCurrentAudio = mAudioList.get(index);
    }

    public static void setCurrentAudio(Audio audio){
        mCurrentAudio = audio;
        mCurrentAudioIndex = mAudioList.indexOf(audio);
    }

    public static Audio getCurrentAudio(){
        if(mAudioList == null){
            getAudioList();
        }
        if(mCurrentAudio == null){
            //读取历史记录
            SharedPreferencesUtil sharedPreferencesUtil = new SharedPreferencesUtil(context, SPNAME);
            mCurrentAudio = sharedPreferencesUtil.getObject(SPKEY_CURRENT_SONG, Audio.class);
            mCurrentAudioIndex = sharedPreferencesUtil.getObject(SPKEY_CURRENT_SONG_INDEX, Integer.class);
        }
        //没有历史记录则取第一首
        if(mCurrentAudioIndex == null && mAudioList.size()>0){
            mCurrentAudio = mAudioList.get(mCurrentAudioIndex = 0);
        }
        return mCurrentAudio;
    }

    public static void addAudio(Audio audio){
        mAudioList.add(audio);
        updateListFlag = true;
    }

    public static void removeAudioById(int id){
        if(id == mCurrentAudio.getId()){
            toNextAudio();
        }
        mAudioList.remove(new Audio(id));
        updateListFlag = true;
    }

    public static void removeAudioByIndex(int index){
        mAudioList.remove(index);
        updateListFlag = true;
    }

    public static Audio getAudioByIndex(int index){
        return mAudioList.get(index);
    }
//
//    public static void removeAudio(int audioId){
//        mAudioList.remove(new Audio(audioId));
//    }

    /**
     * 获取上一首歌
     * @return
     */
    public static Audio getLastAudio(){
        if(mCurrentAudio == null){
            getCurrentAudio();
        }
        if(mAudioList == null){
            return null;
        }
        int lastIndex = (mCurrentAudioIndex - 1 + mAudioList.size()) % mAudioList.size();
        return mAudioList.get(lastIndex);
//        Audio audio = mAudioList.getLast(mCurrentAudio);
//        return audio != null ? audio : mAudioList.get(0);
    }

    /**
     * 获取下一首歌
     * @return
     */
    public static Audio getNextAudio(){
        if(mCurrentAudio == null){
            getCurrentAudio();
        }
        if(mAudioList == null){
            return null;
        }
        int nextIndex = (mCurrentAudioIndex + 1) % mAudioList.size();
        return mAudioList.get(nextIndex);
    }

    public static void toLastAudio(){
        mCurrentAudioIndex = (mCurrentAudioIndex - 1 + mAudioList.size()) % mAudioList.size();
        mCurrentAudio = mAudioList.get(mCurrentAudioIndex);
    }

    public static void toNextAudio(){
        mCurrentAudioIndex = (mCurrentAudioIndex + 1) % mAudioList.size();
        mCurrentAudio = mAudioList.get(mCurrentAudioIndex);
    }

    public static List<Audio> getAudioList(){
        if(mAudioList != null){
            return mAudioList;
        }

        SharedPreferencesUtil sharedPreferencesUtil = new SharedPreferencesUtil(context, SPNAME);
        mAudioList = sharedPreferencesUtil.getObject(SPKEY_SONG_LIST, List.class);
//        mAudioList = MediaUtil.getAudioList(getContext());
        updateListFlag = false;
        if(mAudioList == null){
            mAudioList = MediaUtil.getAudioList(context);
            updateListFlag = true;
        }
        if(mAudioList == null){
            mAudioList = new ArrayList<>();
        }
        return mAudioList;
    }

    public static void saveAudioMessage(){
        SharedPreferencesUtil sharedPreferencesUtil = new SharedPreferencesUtil(context, SPNAME);
        if(updateListFlag){
            sharedPreferencesUtil.setObject(SPKEY_SONG_LIST, mAudioList);
            updateListFlag = false;
        }
        sharedPreferencesUtil.setObject(SPKEY_CURRENT_SONG, mCurrentAudio);
        sharedPreferencesUtil.setObject(SPKEY_CURRENT_SONG_INDEX, mCurrentAudioIndex);
    }


    @Override
    public void onCreate(){
        super.onCreate();
        context = getApplicationContext();
    }
}
